package org.cnc.explain.lifecycle;

import org.cnc.explain.lifecycle.metaconfig_1.MetaConfigAnnotationBean;
import org.cnc.explain.lifecycle.metaconfig_1.MetaConfigBean;
import org.cnc.explain.lifecycle.metaconfig_1.MetaConfigXmlBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * <p>
 * #TODO: desc
 * </p>
 *
 * @author zhiyong.tan
 * @since 2022-08-25
 */
public class Step1BeanMetaConfiguration {

	@Test
	public void testConfigBeanFromXml() {
		FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:step1.xml");
		MetaConfigXmlBean bean = ctx.getBean(MetaConfigXmlBean.class);
		System.out.println(bean);
	}

	@Test
	public void testConfigBeanFromBuilderApi() {
		// assign the bean name
		BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(MetaConfigBean.class.getName());
		builder.addPropertyValue("name", "config from the builder API");
		BeanDefinition bd = builder.getBeanDefinition();
		// 创建spring 容器
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		// 调用registryBeanDefinition向容器中注册bean
		final String beanName = "metaConfigApiBean";
		factory.registerBeanDefinition(beanName, bd);

		// 获取bean
		MetaConfigBean bean = factory.getBean(beanName, MetaConfigBean.class);

		System.out.println(bean.getName());
		System.out.println(bean);
	}


	@Test
	public void testConfigBeanFromAnnotation() {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MetaConfigAnnotationBean.class);
		MetaConfigAnnotationBean bean = ctx.getBean(MetaConfigAnnotationBean.class);
		System.out.println(bean);
	}
}
